<?php

namespace App\Entity;

use App\Enum\LogTypeEnum;
use App\Repository\PageCommentRepository;
use App\Utils\Helper\GetterHelper;
use App\Utils\Helper\LogHelper;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=PageCommentRepository::class)
 * @ORM\HasLifecycleCallbacks()
 */
class PageComment {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="integer")
     */
    private $page_id;

    /**
     * @ORM\Column(type="integer", options={"default"="0"})
     */
    private $parent_id = 0;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $nickname;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $email;

    /**
     * @ORM\Column(type="integer", length=255, nullable=true, options={"default"="0", "comment"="状态：0未审核，1已审核，2已拒绝"})
     */
    private $status = 0;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $comment;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $ip;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updated_at;

    public $parent_nickname;
    public $parent_comment;
    public $parent_email;
    public $parent_ip;
    public $parent_created_at;

    public function getId (): ?int {
        return $this->id;
    }

    public function getPageId (): ?int {
        return $this->page_id;
    }

    public function setPageId (int $page_id): self {
        $this->page_id = $page_id;

        return $this;
    }

    public function getStatus (): ?int {
        return $this->status;
    }

    public function setStatus (?int $status): self {
        $this->status = $status;

        return $this;
    }

    public function getComment (): ?string {
        return $this->comment;
    }

    public function setComment (?string $comment): self {
        $this->comment = $comment;

        return $this;
    }

    public function getIp (): ?string {
        return $this->ip;
    }

    public function setIp (?string $ip): self {
        $this->ip = $ip;

        return $this;
    }

    public function getCreatedAt (): ?\DateTimeInterface {
        return $this->created_at;
    }

    public function setCreatedAt (?\DateTimeInterface $created_at): self {
        $this->created_at = $created_at;

        return $this;
    }

    public function getUpdatedAt (): ?\DateTimeInterface {
        return $this->updated_at;
    }

    public function setUpdatedAt (?\DateTimeInterface $updated_at): self {
        $this->updated_at = $updated_at;

        return $this;
    }

    public function getNickname (): ?string {
        return $this->nickname;
    }

    public function setNickname (?string $nickname): self {
        $this->nickname = $nickname;

        return $this;
    }

    public function getPageAdminUserId () {
        $page = GetterHelper::getEntityManager()->getRepository(Page::class)->find($this->getPageId());
        return is_object($page) ? $page->getAdminUserId() : 0;
    }

    public function getParentId (): ?int {
        return $this->parent_id;
    }

    public function setParentId (int $parent_id): self {
        $this->parent_id = $parent_id;

        return $this;
    }

    public function getParentComment () {
        return GetterHelper::getEntityManager()->getRepository(self::class)->find($this->getParentId());
    }

    public function getEmail (): ?string {
        return $this->email;
    }

    public function setEmail (?string $email): self {
        $this->email = $email;

        return $this;
    }

    /**
     * 删除之前 日志
     * @ORM\PreRemove()
     */
    public function buildLogByDelete () {
        $description = '昵称:['.$this->getNickname().'] 评论:['.$this->getComment().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_COMMENT_DELETE, $description, $this);
    }
}
